<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>在线考试系统-试卷设置</title>
    <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/res/css/style.css" rel="stylesheet">
    <style type="text/css">
        th,td{ text-align: center;}
        /* 删除 模态框 */
        .delete-modal-cancel{margin-left:37px;}
        .delete-modal-ensure{margin-left:40px;}
    </style>
</head>
<body>

<?php include(APPLICATION_PATH.'application/views/index/header.html');?>

<div id="container">

    <div class="container-left">
        <?php include(APPLICATION_PATH.'application/views/index/left.html');?>
    </div>

    <div class="container-right well">
        <div class="tabSet"><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addTestModal">添加试卷</button></div>
        <br/><br/>
        <table class="table table-bordered">
            <thead>
            <tr>
                <th>标题</th>
                <th style="width:150px;">课程</th>
                <th style="width:70px;">题目数</th>
                <th style="width:190px;">添加时间</th>
                <th style="width:190px;">操作</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach($list as $item){ ?>
                <tr>
                    <td><?php echo $item['name'];?></td>
                    <td class="center"><?php echo $item['course_name'];?></td>
                    <td class="center"><?php echo $item['question_count'];?></td>
                    <td class="center"><?php echo $item['update_date'];?></td>
                    <td class="center">
                        <button type="button" class="btn btn-primary showModal" data-toggle="modal" data-target="#showModal" api_url="<?php echo $item['api_url'];?>" api_method="<?php echo $item['api_method'];?>" fields='<?php echo $item["fields"];?>' api_name="<?php echo $item['api_name'];?>">查看</button>
                        <button type="button" class="btn btn-primary editTestModal" data-toggle="modal" data-target="#updateTestModal" test_title="<?php echo $item['name'];?>" _id="<?php echo $item['_id'];?>">编辑</button>
                        <button type="button" class="btn btn-danger deleteTestModal" data-toggle="modal" data-target="#removeTestModal" _id="<?php echo $item['_id'];?>">删除</button>
                    </td>
                </tr>
            <?php }?>
            </tbody>
        </table>
        <!--
        <?php echo $pagers;?>
        -->
    </div>

</div>

<div class="modal fade" id="addTestModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">添加试卷</h4>
            </div>
            <br/>
            <div class="modal-body">
                <h5>标题</h5>
                <input class="form-control" type="text" id="test_name" placeholder="" style="width:200px;">
                <div class="form-group">
                    <label for="status" class="control-label">课程</label>
                    <select select="" type="text" class="form-control" id="course_id" name="course_id" style="width:202px;">
                        <?php foreach($courses as $item){ ?>
                            <option value="<?php echo $item['_id'];?>"><?php echo $item['name'];?></option>
                        <?php }?>
                    </select>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="addTest" class="btn btn-primary"> 保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="modal fade" id="updateTestModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">修改试卷</h4>
            </div>
            <div class="modal-body">
                <input type="hidden" id="update_data_id" value=""/>
                <h5>试卷标题</h5>
                <input class="form-control" type="text" id="update_test_name" placeholder="" style="width:200px;">
                <br/>
                <select select="" type="text" class="form-control" id="update_course_id" name="course_id" style="width:202px;">
                    <?php foreach($courses as $item){ ?>
                        <option value="<?php echo $item['_id'];?>"><?php echo $item['name'];?></option>
                    <?php }?>
                </select>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="saveCourse" class="btn btn-primary"> 保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- delete Test Model -->
<div class="modal fade bs-delete-modal-sm" id="removeTestModal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="mySmallModalLabel">删除试卷</h4>
            </div>
            <input type="hidden" id="delete_data_id" value=""/>
            <div class="modal-body">
                <button type="button" class="btn btn-default delete-modal-cancel" data-dismiss="modal">&nbsp;取&nbsp;&nbsp;消&nbsp;&nbsp;</button>
                <button type="button" class="btn btn-danger delete-modal-ensure">&nbsp;&nbsp;删&nbsp;&nbsp;除&nbsp;&nbsp;</button>
            </div>
        </div><!-- /.modal-content -->
    </div>
</div>

<script src="/res/js/jquery.min.js"></script>
<script src="/bootstrap/js/bootstrap.min.js"></script>

<script src="/res/js/toast.js"></script>

<script>
    $(function(){

        $('#addTest').click(function(){
            var test_name = $('#test_name').val();
            var course_id = $('#course_id').val();

            if(test_name =='') {
                new Toast({context:$('body'),top:200 ,time:2000, message:'试卷名称不能为空'}).error();
                return false;
            }

            if(course_id =='') {
                new Toast({context:$('body'),top:200 ,time:2000, message:'课程不能为空'}).error();
                return false;
            }

            var url = '/test/add';
            var data = {"name":test_name, "course_id":course_id};
            $.post(url, data, function(ret){
                if(ret.en == 200) {
                    new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).success();
                    setTimeout(function(){
                        window.location.href = '/test/index';
                    } , 2000);
                } else {
                    new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).error();
                }
            });
        });

        /** "修改" 触发模态框显示 */
        $('.editTestModal').click(function(){
            var _id = $(this).attr('_id');
            $('#update_data_id').val(_id);

            var test_title = $(this).attr('test_title');
            $('#update_test_name').val(test_title);
        });

        $('#saveCourse').click(function(){
            var update_data_id = $('#update_data_id').val();
            var update_test_name = $('#update_test_name').val();
            var update_course_id = $('#update_course_id').val();

            if(update_data_id == '') {
                new Toast({context:$('body'),top:200 ,time:2000, message:'试卷不能为空'}).error();
                return false;
            }
            if(update_test_name =='') {
                new Toast({context:$('body'),top:200 ,time:2000, message:'试卷标题不能为空'}).error();
                return false;
            }
            if(update_course_id =='') {
                new Toast({context:$('body'),top:200 ,time:2000, message:'课程不能为空'}).error();
                return false;
            }

            var url = '/test/save';
            var data = {"_id":update_data_id, "name":update_test_name, "course_id":update_course_id};
            $.post(url, data, function(ret){
                if(ret.en == 200) {
                    new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).success();
                    setTimeout(function(){
                        window.location.href = '/test/index';
                    } , 2000);
                } else {
                    new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).error();
                }
            });
        });

        /** "删除" 触发模态框显示 */
        $('.deleteTestModal').click(function(){
            var _id = $(this).attr('_id');
            $('#delete_data_id').val(_id);
        });

        /** 执行 "删除" */
        $('.delete-modal-ensure').click(function(){
            var _id = $('#delete_data_id').val();
            var request_api = '/test/delete';
            if(_id != undefined && _id != '') {
                $.post(request_api ,{"_id":_id} ,function(ret) {
                    if(ret.en == 200) {
                        new Toast({context:$('body'),top:200 ,time:1000 ,message:ret.em}).success();
                        setTimeout(function(){
                            $('#removeArticleModal').modal('hide');
                            window.location.reload();
                        } , 1000);
                    } else {
                        new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).error();
                    }
                });
            } else {
                new Toast({context:$('body'),top:200 ,time:1000, message:"参数不全"}).error();
            }
        });

    });
</script>

</body>
</html>